package me.zhengjie.modules.management.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.management.domain.MrConferenceRoomEquips;
import me.zhengjie.modules.management.domain.Pageable;
import me.zhengjie.modules.management.mapper.MrConferenceRoomEquipsMapper;
import me.zhengjie.modules.management.service.MrConferenceRoomEquipsService;
import me.zhengjie.modules.management.service.dto.MrConferenceRoomEquipsCriteria;
import me.zhengjie.utils.PageHelpResultUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: eladmin
 * @description: 会议室设备业务逻辑实现
 * @author: ming.cao
 * @create: 2020-12-05 14:22
 **/

@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MrConferenceRoomEquipsServiceImpl implements MrConferenceRoomEquipsService {

    private final MrConferenceRoomEquipsMapper mrConferenceRoomEquipsMapper;

    @Override
    public Map<String, Object> getAllList(MrConferenceRoomEquipsCriteria criteria, Pageable pageable) {
        PageHelper.startPage(pageable.getPage(),pageable.getSize());
        List<MrConferenceRoomEquips> equipsList = mrConferenceRoomEquipsMapper.getAllList(criteria);
        //分页数据
        PageInfo<MrConferenceRoomEquips> pageInfo1 = new PageInfo<>(equipsList);
        return PageHelpResultUtil.toPage(pageInfo1);
    }

    @Override
    public Map<String, Object> getMrConferenceRoomEquipsById(Integer id) {
        MrConferenceRoomEquips equips = mrConferenceRoomEquipsMapper.getMrConferenceRoomEquipsById(id);
        Map<String,Object> map = new LinkedHashMap<>(2);
        map.put("content",equips);
        return map;
    }

    @Override
    public void addConferenceRoomEquips(MrConferenceRoomEquips mrConferenceRoomEquips) {
        try{
            mrConferenceRoomEquips.setCreateDateTime(new Timestamp(new Date().getTime()));
            mrConferenceRoomEquips.setCreateUserId(SecurityUtils.getCurrentUsername());
            mrConferenceRoomEquipsMapper.addConferenceRoomEquips(mrConferenceRoomEquips);
        }catch (Exception e){
            throw new BadRequestException("新增失败！");
        }


    }

    @Override
    public void updateConferenceRoomEquips(MrConferenceRoomEquips mrConferenceRoomEquips) {
        try{
            mrConferenceRoomEquips.setModifyDateTime(new Timestamp(new Date().getTime()));
            mrConferenceRoomEquips.setModifyUserId(SecurityUtils.getCurrentUsername());
            mrConferenceRoomEquipsMapper.updateConferenceRoomEquips(mrConferenceRoomEquips);
        }catch (Exception e){
            throw new BadRequestException("更新失败！");
        }

    }

    @Override
    @Transactional
    public void delConferenceRoomEquips(List<Integer> ids) {
        try{
            for (Integer id : ids) {
                mrConferenceRoomEquipsMapper.delConferenceRoomEquips(id);
            }
        }catch (Exception e){
            throw new BadRequestException("删除失败！");
        }

    }

}
